var locationAnalyser ={
		init :function(){
			/*var pointSimplifierIns;*/
			this.vehicleMap=new HashMap();
			this.groupStyleMap =new HashMap();
			this.time = 10 * 60 * 1000;			//定时器时间，10分钟刷新一下
			this.mapZoom = 12;
			this.infoWindow=null;
			this.marker=null;
			this.loadMap();
			this.loadUI();	
			this.loadTrade();
			this.loadVehicles();
			this.mapHeight();
		},
		loadVehicles : function(){
			  global.ajaxLoading.pop("正在加载数据...",true);
			$.ajax({
				url : global.url.domain+'gps-web/gis/location/getVehicleInfo.html',
				dataType : 'json',
				type : 'post',
				success : function(da){
					if(da.code != 10002){
						var data =da.data;
						for(var i=0;i<data.length;i++){
							var key=data[i].vehicle_no+":"+data[i].vehicle_color;
							locationAnalyser.vehicleMap.put(key,data[i]);
						}
						
					}
				}
			});
		},
		loadTrade: function(){
			$('#selectTrade').select2({
				 minimumResultsForSearch: -1,
				data: [{id: 0, text: '请选择行业类型'},{id: 1, text: '班线客运'},{id: 2, text: '旅游客运'},{id: 3, text: '危运车辆'}]
			});
			$('#selectAlarm').select2({
				 minimumResultsForSearch: -1,
				data: [{id: 1, text: '超速报警'},{id: 2, text: '疲劳报警'},{id: 3, text: '停车车辆'}]
			});
			$("#selectTrade").next().css("border","none");
			$("#selectAlarm").next().css("border","none");
		},
		loadMap : function(){
			 var  _this=this;// 创建Map实例
            var center=localStorage.getItem('cityMapCenter').split(',')
	         if($("#mymaps").length<=0)return;
	         this.map = new AMap.Map('mymaps',{
	             resizeEnable: true,
	             zoom: _this.mapZoom,
	             center: center
	         });  
	         AMap.plugin(['AMap.ToolBar'],function(){
	        	 _this.map.addControl(new AMap.ToolBar());
	         });
	         /*var LngLat = [{Lng:"106.674231",Lat:"26.619282"},
	                       {Lng:"106.630153",Lat:"26.647661"},
	                       {Lng:"106.620438",Lat:"26.613164"},
	                       {Lng:"106.710687",Lat:"26.572875"}
	                      ];
	         for(var i=0;i<LngLat.length;i++){
	        	 locationAnalyser.addCircle(LngLat[i]);
	         }*/
		},
		

		
		loadUI : function(){
		
			 var  _this=this;
			 
			 
			 
			  AMapUI.load(['ui/misc/PointSimplifier', 'lib/$'], function(PointSimplifier, $) {

			        if (!PointSimplifier.supportCanvas) {
			        	global.ajaxLoading.pop('当前环境不支持 Canvas！', true, 2000);
			            return;
			        }
			       
			        var pointSimplifierIns = new PointSimplifier({

			            zIndex: 300,
			            map:_this.map,
			            autoSetFitView : false,
			            getPosition: function(item) {

			                if (!item) {
			                    return null;
			                }

			                return [parseFloat(item.lngLatLine.lng), parseFloat(item.lngLatLine.lat)];
			            },
			            getHoverTitle: function(dataItem, idx) {
			            	var vehicleNo = dataItem.lngLatLine.id.split(":");
			                return '车牌号: ' + vehicleNo[0];
			            },
			            renderConstructor: PointSimplifier.Render.Canvas.GroupStyleRender,
			            
			        
			                renderOptions: {
			                    getGroupId: function(item, idx) {
			                        return item.groupId;
			                    },
			                    groupStyleOptions: function(gid) {
			                    	return _this.groupStyleMap.get(gid);
			                    }

			                }
			             
			        });
			        function onIconLoad() {
			            pointSimplifierIns.renderLater();
			        }

			        function onIconError(e) {
			        	global.ajaxLoading.pop('图片加载失败！', true, 2000);
			        }
			        
			        window.pointSimplifierIns = pointSimplifierIns;
			        global.ajaxLoading.pop("正在加载数据...",true);
			        requireAjax = function(){
						 $.ajax({
								url : global.url.domain+'gps-web/gis/location/position.html',
								data : locationAnalyser.getParam(),
								dataType : 'JSON',
								type : 'post',
								success : function(data){
									if(window.pointSimplifierIns){
										window.pointSimplifierIns.setData(null);
									}
									var csv = [];
									for(var i=0;i<data.length;i++){
										var lngLatLine = data[i].lonLat ;
										var vehicle =locationAnalyser.vehicleMap.get(data[i].id);
										var head = mapTool.getHead(data[i].head);
										var groupId = data[i].alarm+":"+head;
										
										
										var imgSrc;
										var alarm = 'speed';
										switch (data[i].alarm){
											case 1:
												imgSrc = global.url.domain+'gps-web/resources/images/mapTool/red'+head+'.png';
												break;
											case 2:
												imgSrc = global.url.domain+'gps-web/resources/images/mapTool/carYellow'+head+'.png';
												break;
											case 3:
												imgSrc = global.url.domain+'gps-web/resources/images/mapTool/gray'+head+'.png';
												break;
										};
										
										_this.groupStyleMap.put(groupId,  {
												pointStyle: {
							                        //绘制点占据的矩形区域
							                        content: PointSimplifier.Render.Canvas.getImageContent(
							                        		imgSrc, onIconLoad),
							                        //宽度
							                        width: 32,
							                        //高度
							                        height: 32,
							                        //定位点为中心
							                        offset: ['-50%', '-50%'],
							                        fillStyle: null,
							                        //strokeStyle: null
							                    }
										});
										csv.push({
						                    lngLatLine: data[i],
						                    groupId: groupId
						                });
									}
							       	window.pointSimplifierIns.setData(csv);
							        global.ajaxLoading.close();
								}
							});
					 };
			        requireAjax();
			        timer = setInterval(requireAjax,_this.time);
			        pointSimplifierIns.on('pointClick ', function(e, record) {
			        	var vehicle = locationAnalyser.vehicleMap.get(record.data.lngLatLine.id);
			        	var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, 0)});
			        	var content='车牌号：'+vehicle.vehicle_no+"-"+vehicle.vehicle_color_name+'</br>所属业户：'+vehicle.enterprise_name+'</br>所属行业：'+vehicle.tradeName+'</br>GPS速度：'+record.data.lngLatLine.gpsSpeed+'  (Km/h)';
			        	infoWindow.setContent(content);
			        	infoWindow.open(_this.map,e.originalEvent.lnglat);
			        	
				    });
			    });
		},
		getParam : function(){
			var tradeId = $('#selectTrade').val();
			var alarmId = $('#selectAlarm').val();
			param ={
				tradeId : tradeId,
				status : alarmId
			};
			return param;
		},
		bindQuery : function(){
			var _this = this;
			clearInterval(timer);
			requireAjax();
		},
		mapHeight:function(){
			var height=$(document).height()-135+'px';
			$('#mymaps').height(height);
		},
		//添加圆覆盖物  
		addCircle : function(LngLat) {  
		   var point = new AMap.LngLat(LngLat.Lng,LngLat.Lat);
		   circle = new AMap.Circle({   
		   center:point,// 圆心位置  
		   radius:1000, //半径  
		   strokeColor: "#F33", //线颜色  
		   strokeOpacity: 1, //线透明度  
		   strokeWeight: 3, //线粗细度  
		   fillColor: "#ee2200", //填充颜色  
		   fillOpacity: 0.35//填充透明度  
		   });   
		   circle.setMap(this.map); 
		  
		} 
		
};
locationAnalyser.init();